using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MBMC.Paths;

namespace MBMC.Dialogs
{
    public partial class Sound_and_Music : Form
    {
        string mbfolder;
        Paths.Paths p = new MBMC.Paths.Paths();

        System.Media.SoundPlayer sppp;

        public Sound_and_Music(string actualmbfolder, bool isadacityinstalled)
        {
            InitializeComponent();
            mbfolder = actualmbfolder;
            if(isadacityinstalled== true)
            {
              button2.Enabled = true;
              this.label3.ForeColor = System.Drawing.Color.Green;
              this.label3.Text = "Audacity plugin installed!";
            }
        }

        private void Sound_and_Music_Load(object sender, EventArgs e)
        {
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(p.Vrat_Cestu(mbfolder,MBPaths.FMDSound));
            foreach (System.IO.FileInfo file in di.GetFiles("*.wav"))
            {
                Console.WriteLine("{0}, {1}, {2}", file.Name, file.Length , file.Extension);
                listBox1.Items.Add(file.Name);
            }
            foreach (System.IO.FileInfo file in di.GetFiles("*.ogg"))
            {
                Console.WriteLine("{0}, {1}, {2}", file.Name, file.Length, file.Extension);
                listBox2.Items.Add(file.Name);
            }
        }

        public void PlayWAV(string path)
        {
            System.Media.SoundPlayer sp = new System.Media.SoundPlayer(path);
            sp.Play();
        }

        public void PlayOGG(string path)
        {
            //docasne
            //process1.StartInfo.Arguments = path;
            //process1.Start();
            try
            {
                ///////////////////////
                //Mato Kormuth's Code//
                ///////////////////////
                MBMC.Paths.Paths p = new MBMC.Paths.Paths();
                //copy file to temp
                //System.IO.File.Copy(textBox1.Text, Application.StartupPath + "/temp/oggrenders/" + "current.mbmcsound", true);
                System.IO.File.Copy(path, Application.StartupPath + "/towav.ogg", true);
                //run oggenc
                process3.StartInfo.FileName = "oggdec.exe";
                process3.StartInfo.Arguments = "towav.ogg";
                process3.Start();
                //select name for library 
                //MessageBox.Show("Now select the name to save song in game(without the extension: .ogg)");
                MessageBox.Show("Now click to OK!");
                sppp = new System.Media.SoundPlayer(Application.StartupPath + "/towav.wav");
                sppp.Play();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
            
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //change sounds documentation
            linkLabel1.Text = p.Vrat_Cestu(mbfolder, MBPaths.FMDSound) + "/" + listBox1.SelectedItem.ToString();
            label1.Text = listBox1.SelectedItem.ToString();
            richTextBox2.Text = linkLabel1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PlayWAV(p.Vrat_Cestu(mbfolder, MBPaths.FMDSound) + "/" + listBox1.SelectedItem.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            PlayOGG(p.Vrat_Cestu(mbfolder, MBPaths.FMDSound) + "/" + listBox2.SelectedItem.ToString());
        }

        private void label4_Click(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            process1.StartInfo.FileName = Application.StartupPath + process1.StartInfo.FileName;
            process1.Start();
            
        }

        private void button12_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Gallery is disconected");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            textBox1.Text = openFileDialog1.FileName;

        }

        private void button9_Click(object sender, EventArgs e)
        {
            ///////////////////////
            //Mato Kormuth's Code//
            ///////////////////////
            MBMC.Paths.Paths p = new MBMC.Paths.Paths();
            //copy file to temp
            //System.IO.File.Copy(textBox1.Text, Application.StartupPath + "/temp/oggrenders/" + "current.mbmcsound", true);
            System.IO.File.Copy(textBox1.Text,"C:/mbmctmp/current.mbmcsound", true);
            //run oggenc
            process2.StartInfo.FileName = Application.StartupPath + "/temp/oggrenders/" + "oggenc.exe";
            process2.StartInfo.Arguments = "C:/mbmctmp/current.mbmcsound";
            process2.Start();
            //select name for library 
            //MessageBox.Show("Now select the name to save song in game(without the extension: .ogg)");
            MessageBox.Show("Now click to OK!");
            System.IO.File.Copy("C:/mbmctmp/current.ogg", p.Vrat_Cestu(mbfolder, MBPaths.FMDSound) + "/" + textBox2.Text + ".ogg", true);
            System.IO.File.Delete("C:/mbmctmp/current.mbmcsound");
            System.IO.File.Delete("C:/mbmctmp/current.ogg");
            MessageBox.Show("Completed, to show new information dialog must be restarted.");
            this.Close();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            openFileDialog2.ShowDialog();
        }

        private void openFileDialog2_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                System.IO.FileInfo fii = new System.IO.FileInfo(openFileDialog2.FileName);
                System.IO.File.Copy(openFileDialog2.FileName, mbfolder + MBMC.Paths.MBPaths.FMDSound + "/" + fii.Name);
            }
            catch (Exception exec)
            {
                MessageBox.Show(exec.ToString());
            }
            //copyed

        }

        private void Sound_and_Music_FormClosing(object sender, FormClosingEventArgs e)
        {
            ////MATO KORMUTH CODE//////
            ///Temp files delete code//
            //Version 1.0//////////////
            int pocet = 0;
            int dlzkaspolu = 0;
            //e.Cancel = true;
            if (System.IO.File.Exists("C:/mbmctmp/towav.wav") == true)
            {
                System.IO.FileInfo ioo = new System.IO.FileInfo("C:/mbmctmp/towav.wav");
                pocet += 1;
                dlzkaspolu += (int)ioo.Length;
            }
            if (System.IO.File.Exists("C:/mbmctmp/towav.ogg") == true)
            {
                System.IO.FileInfo iooo = new System.IO.FileInfo("C:/mbmctmp/towav.ogg");
                pocet += 1;
                dlzkaspolu += (int)iooo.Length;
            }
            if (System.IO.File.Exists("C:/mbmctmp/current.mbmcsound") == true)
            {
                System.IO.FileInfo ioooo = new System.IO.FileInfo("C:/mbmctmp/current.mbmcsound");
                pocet += 1;
                dlzkaspolu += (int)ioooo.Length;
            }
            if (System.IO.File.Exists("C:/mbmctmp/current.ogg") == true)
            {
                System.IO.FileInfo ioot = new System.IO.FileInfo("C:/mbmctmp/current.ogg");
                pocet += 1;
                dlzkaspolu += (int)ioot.Length;
            }
            if (System.IO.File.Exists(Application.StartupPath + "/current.ogg") == true)
            {
                System.IO.FileInfo ioot = new System.IO.FileInfo(Application.StartupPath + "/current.ogg");
                pocet += 1;
                dlzkaspolu += (int)ioot.Length;
            }
            if (System.IO.File.Exists(Application.StartupPath + "/current.mbmcsound") == true)
            {
                System.IO.FileInfo ioooo = new System.IO.FileInfo(Application.StartupPath + "/current.mbmcsound");
                pocet += 1;
                dlzkaspolu += (int)ioooo.Length;
            }
            if (System.IO.File.Exists(Application.StartupPath + "/towav.wav") == true)
            {
                System.IO.FileInfo ioo = new System.IO.FileInfo(Application.StartupPath + "/towav.wav");
                pocet += 1;
                dlzkaspolu += (int)ioo.Length;
            }
            if (System.IO.File.Exists(Application.StartupPath + "/towav.ogg") == true)
            {
                System.IO.FileInfo iooo = new System.IO.FileInfo(Application.StartupPath + "/towav.ogg");
                pocet += 1;
                dlzkaspolu += (int)iooo.Length;
            }
            if (pocet == 0)
            {
                //cl9ose form
                e.Cancel = false;

            }
            else
            {
                DialogResult dr = MessageBox.Show("MBMC has detected " + pocet + " temp files, size: " + dlzkaspolu + " bytes. Do you wish to remove temp files?", "Mato Programs", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (dr == DialogResult.Yes)
                {
                    //remove files
                    System.IO.File.Delete("C:/mbmctmp/current.ogg");
                    System.IO.File.Delete("C:/mbmctmp/current.mbmcsound");
                    System.IO.File.Delete("C:/mbmctmp/towav.ogg");
                    System.IO.File.Delete("C:/mbmctmp/towav.wav");
                    System.IO.File.Delete(Application.StartupPath + "/current.ogg");
                    System.IO.File.Delete(Application.StartupPath + "/current.mbmcsound");
                    System.IO.File.Delete(Application.StartupPath + "/towav.ogg");
                    System.IO.File.Delete(Application.StartupPath + "/towav.wav");
                }
                else
                {
                    //close form

                }
            }
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void button14_Click(object sender, EventArgs e)
        {
            sppp.Stop();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
        }
    }
}